package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.query.CustomerTransferQueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

	@Autowired
	private CustomerTransferMapper customerTransferMapper;

	@Autowired
	private CustomerMapper customerMapper;

	@Override
	public void deleteByPrimaryKey(Long id) {

		customerTransferMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void insert(CustomerTransfer entity) {
		customerMapper.updateSeller(entity.getNewSeller().getId(), Customer.STATUS_COMMON,entity.getCustomer().getId());
		entity.setOperator(UserContext.getCurrentUser());
		entity.setOperateTime(new Date());
		customerTransferMapper.insert(entity);
	}

	@Override
	public CustomerTransfer selectByPrimaryKey(Long id) {
		
		return customerTransferMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<CustomerTransfer> selectAll() {
		
		return customerTransferMapper.selectAll();
	}

	@Override
	public void updateByPrimaryKey(CustomerTransfer entity) {

		customerTransferMapper.updateByPrimaryKey(entity);
	}

	@Override
	public PageInfo query(CustomerTransferQueryObject qo) {
		//查询总记录数

		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
		List<CustomerTransfer> data = customerTransferMapper.queryForList(qo);
		return new PageInfo<CustomerTransfer>(data);
	}

}
